package com.example.yangyang.widgettest_125yy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //声明控件
    private TextView tv;
    private EditText et;
    private Button btngettext,btnseting;
    private ImageButton ibtn;
    private ImageView iv;

    private RadioButton rbnan,rbnv;
    private RadioGroup rg;
    private CheckBox cblan,cbzu,cbyou;
    private Button btnsetgender,btngetfun;
    private String strfun=null;// 保存兴趣的字符串
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件
        init();
    }

    private void init() {
        tv=(TextView)findViewById(R.id.textView2);
        et=(EditText)findViewById(R.id.editText2);
        btngettext=(Button)findViewById(R.id.button);
        btnseting=(Button)findViewById(R.id.button2);
        ibtn=(ImageButton)findViewById(R.id.imageButton5);
        iv=(ImageView)findViewById(R.id.imageView);


        btngettext.setOnClickListener(this);
        btnseting.setOnClickListener(this);
        ibtn.setOnClickListener(this);

        rbnan=(RadioButton)findViewById(R.id.radioButton3);
        rbnv=(RadioButton)findViewById(R.id.radioButton3);
        rg =(RadioGroup) findViewById(R.id.radiogroup);
        cblan =(CheckBox) findViewById(R.id.checkBox3);
        cbzu =(CheckBox) findViewById(R.id.checkBox2);
        cbyou =(CheckBox) findViewById(R.id.checkBox);
        btnsetgender=(Button)findViewById(R.id.button6);
        btngetfun=(Button)findViewById(R.id.button8);

        btnsetgender.setOnClickListener(this);
        btngetfun.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group,
                                         @IdRes  int i) {
                switch (i)
                {
                    case R.id.radioButton4:
                        tv.setText("女");
                        break;
                    case R.id.radioButton3:
                        tv.setText("男");
                        break;
                }
            }
        });
        //选择改变事件
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean b) {
                if(b)
                    strfun+="篮球+";
                else
                    strfun=strfun.replace("篮球+","");
                tv.setText(strfun);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean f) {
                if(f)
                    strfun+="足球+";
                else
                    strfun=strfun.replace("足球+","");
                tv.setText(strfun);
            }
        });
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean s) {
                if(s)
                    strfun+="游泳+";
                else
                    strfun=strfun.replace("游泳+","");
                tv.setText(strfun);
            }
        });

    }
    public void onClick(View view){
        switch (view.getId())
        {
            case R.id.button:
                String str=et.getText().toString();
                tv.setText(str);
                break;
            case R.id.imageButton5:
                Intent intent=new Intent(MainActivity.this,NewActivity.class);
                intent.putExtra("param1",et.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);
                break;
            case R.id.button6://性别
                if(rbnan.isChecked())
                    tv.setText("男");

                if(rbnv.isChecked())
                    tv.setText("男");
                break;
            case R.id.button8://兴趣
                String msg="";
                if(cblan.isChecked())
                    msg+="篮球+";
                if(cbzu.isChecked())
                    msg+="足球+";
                if(cbyou.isChecked())
                    msg+="游泳+";
                tv.setText(msg);
                break;
        }
    }


}
